package com.ciwong.epaper.modules.epaper.ui.bookcase;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.bean.AddEvent;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.modules.epaper.ui.EpaperFragment;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.util.DisplayUtils;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.eventbus.EventBus;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import java.util.List;

/**
 * Created by Administrator on 2017/8/22.
 */

public class ItemBookCase extends LinearLayout implements View.OnClickListener, View.OnLongClickListener {
    private final String TAG = "EpaperFragment";

    private SimpleDraweeView book_desk_img1;
    private SimpleDraweeView book_desk_img2;
    private SimpleDraweeView book_desk_img3;
    private LinearLayout book_desk_img_handle1;
    private LinearLayout book_desk_img_handle2;
    private LinearLayout book_desk_img_handle3;
    private RelativeLayout book_desk_img_parent1;
    private RelativeLayout book_desk_img_parent2;
    private RelativeLayout book_desk_img_parent3;
    private TextView tx_book_name1;
    private TextView tx_book_name2;
    private TextView tx_book_name3;
    private List<EpaperInfo> mEpaperInfoList;
    private Fragment mFragment;
    private float bookWidth;
    private float bookHeight;
    private int itemWidth, height;

    private LinearLayout item_book_case;

    private ImageView img_kaoshi1;
    private ImageView img_kaoshi2;
    private ImageView img_kaoshi3;
    private ImageView img_free1;
    private ImageView img_free2;
    private ImageView img_free3;


    private CheckBox check_box1;
    private CheckBox check_box2;
    private CheckBox check_box3;

    private View check_box_handle1;
    private View check_box_handle2;
    private View check_box_handle3;


    private View add_entran_handle1;
    private View add_entran_handle2;
    private View add_entran_handle3;

    private boolean mIsEidtMode;

    public ItemBookCase(Context context, Fragment fragment, float width, float height) {
        super(context);

        itemWidth = getResources().getDisplayMetrics().widthPixels;
        bookWidth = width;
        bookHeight = height;
        if (EApplication.PLATFORM_ENV == EApplication.PLATFORM_PAD) {
            LayoutInflater.from(this.getContext()).inflate(R.layout.item_book_case_pad, this);
        } else {
            LayoutInflater.from(this.getContext()).inflate(R.layout.item_book_case, this);
        }


        book_desk_img1 = (SimpleDraweeView) findViewById(R.id.book_desk_img1);
        book_desk_img2 = (SimpleDraweeView) findViewById(R.id.book_desk_img2);
        book_desk_img3 = (SimpleDraweeView) findViewById(R.id.book_desk_img3);

        book_desk_img_handle1 = (LinearLayout) findViewById(R.id.book_desk_img_handle1);
        book_desk_img_handle2 = (LinearLayout) findViewById(R.id.book_desk_img_handle2);
        book_desk_img_handle3 = (LinearLayout) findViewById(R.id.book_desk_img_handle3);

        book_desk_img_parent1 = (RelativeLayout) findViewById(R.id.book_desk_img_parent1);
        book_desk_img_parent2 = (RelativeLayout) findViewById(R.id.book_desk_img_parent2);
        book_desk_img_parent3 = (RelativeLayout) findViewById(R.id.book_desk_img_parent3);

        check_box_handle1 = findViewById(R.id.check_box_handle1);
        check_box_handle2 = findViewById(R.id.check_box_handle2);
        check_box_handle3 = findViewById(R.id.check_box_handle3);

        img_kaoshi1 = (ImageView) findViewById(R.id.img_kaoshi1);
        img_kaoshi2 = (ImageView) findViewById(R.id.img_kaoshi2);
        img_kaoshi3 = (ImageView) findViewById(R.id.img_kaoshi3);

        img_free1 = (ImageView) findViewById(R.id.img_free1);
        img_free2 = (ImageView) findViewById(R.id.img_free2);
        img_free3 = (ImageView) findViewById(R.id.img_free3);


        check_box1 = (CheckBox) findViewById(R.id.check_box1);
        check_box2 = (CheckBox) findViewById(R.id.check_box2);
        check_box3 = (CheckBox) findViewById(R.id.check_box3);
//        check_box1.sty

        add_entran_handle1 = findViewById(R.id.add_entran_handle1);
        add_entran_handle2 = findViewById(R.id.add_entran_handle2);
        add_entran_handle3 = findViewById(R.id.add_entran_handle3);

        add_entran_handle1.setOnClickListener(this);
        add_entran_handle2.setOnClickListener(this);
        add_entran_handle3.setOnClickListener(this);

        item_book_case = (LinearLayout) findViewById(R.id.item_book_case);
        ViewGroup.LayoutParams layoutParams = item_book_case.getLayoutParams();
        layoutParams.width = itemWidth;
        item_book_case.setLayoutParams(layoutParams);
        LinearLayout.LayoutParams paramsParent;

        paramsParent = (LinearLayout.LayoutParams) book_desk_img_parent1.getLayoutParams();
        paramsParent.width = (int) bookWidth;
        paramsParent.height = (int) bookHeight;
        book_desk_img_parent1.setLayoutParams(paramsParent);

        paramsParent = (LinearLayout.LayoutParams) book_desk_img_parent2.getLayoutParams();
        paramsParent.width = (int) bookWidth;
        paramsParent.height = (int) bookHeight;
        book_desk_img_parent2.setLayoutParams(paramsParent);
        paramsParent = (LinearLayout.LayoutParams) book_desk_img_parent3.getLayoutParams();
        paramsParent.width = (int) bookWidth;
        paramsParent.height = (int) bookHeight;
        book_desk_img_parent3.setLayoutParams(paramsParent);


        RelativeLayout.LayoutParams params;
        params = (RelativeLayout.LayoutParams) book_desk_img1.getLayoutParams();
        params.width = (int) bookWidth;
        params.height = (int) bookHeight;
        book_desk_img1.setLayoutParams(params);

        params = (RelativeLayout.LayoutParams) book_desk_img2.getLayoutParams();
        params.width = (int) bookWidth;
        params.height = (int) bookHeight;
        book_desk_img2.setLayoutParams(params);

        params = (RelativeLayout.LayoutParams) book_desk_img3.getLayoutParams();
        params.width = (int) bookWidth;
        params.height = (int) bookHeight;
        book_desk_img3.setLayoutParams(params);


        book_desk_img_handle1.setOnClickListener(this);
        book_desk_img_handle2.setOnClickListener(this);
        book_desk_img_handle3.setOnClickListener(this);
//        book_desk_img_handle1.setOnLongClickListener(this);
//        book_desk_img_handle2.setOnLongClickListener(this);
//        book_desk_img_handle3.setOnLongClickListener(this);

        tx_book_name1 = (TextView) findViewById(R.id.tx_book_name1);
        tx_book_name2 = (TextView) findViewById(R.id.tx_book_name2);
        tx_book_name3 = (TextView) findViewById(R.id.tx_book_name3);
        mFragment = fragment;


        LinearLayout.LayoutParams txNamePara;
        txNamePara = (LayoutParams) tx_book_name1.getLayoutParams();
        txNamePara.width = (int) bookWidth;
        tx_book_name1.setLayoutParams(txNamePara);

        txNamePara = (LayoutParams) tx_book_name2.getLayoutParams();
        txNamePara.width = (int) bookWidth;
        tx_book_name2.setLayoutParams(txNamePara);

        txNamePara = (LayoutParams) tx_book_name3.getLayoutParams();
        txNamePara.width = (int) bookWidth;
        tx_book_name3.setLayoutParams(txNamePara);


        check_box1.setOnCheckedChangeListener(onCheckedChangeListener);
        check_box2.setOnCheckedChangeListener(onCheckedChangeListener);
        check_box3.setOnCheckedChangeListener(onCheckedChangeListener);


    }


    CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int id = buttonView.getId();

            Log.d(TAG, "######onCheckedChanged#####" + isChecked);

            AddEvent addEvent = new AddEvent(EpaperFragment.ADD_DELETE_BOOK);
            addEvent.setSelect(isChecked);

            if (id == R.id.check_box1) {

                addEvent.setProductId(mEpaperInfoList.get(0).getProductId());
                addEvent.setEpaperInfo(mEpaperInfoList.get(0));
                EventBus.getDefault().post(addEvent);


            } else if (id == R.id.check_box2) {

                addEvent.setProductId(mEpaperInfoList.get(1).getProductId());
                addEvent.setEpaperInfo(mEpaperInfoList.get(1));

                EventBus.getDefault().post(addEvent);

            } else if (id == R.id.check_box3) {

                addEvent.setProductId(mEpaperInfoList.get(2).getProductId());
                addEvent.setEpaperInfo(mEpaperInfoList.get(2));

                EventBus.getDefault().post(addEvent);

            }

        }
    };


    public void update(List<EpaperInfo> epaperInfoList, boolean isEditMode) {


        mIsEidtMode = isEditMode;
        if (null != epaperInfoList && epaperInfoList.size() > 0) {
            Uri uri;
            mEpaperInfoList = epaperInfoList;
            add_entran_handle1.setVisibility(GONE);
            add_entran_handle2.setVisibility(GONE);
            add_entran_handle3.setVisibility(GONE);

            if (isEditMode) {
                check_box_handle1.setVisibility(VISIBLE);
                check_box_handle2.setVisibility(VISIBLE);
                check_box_handle3.setVisibility(VISIBLE);

            } else {

                check_box_handle1.setVisibility(GONE);
                check_box_handle2.setVisibility(GONE);
                check_box_handle3.setVisibility(GONE);
            }

            switch (epaperInfoList.size()) {
                case 1:
                    book_desk_img_handle1.setVisibility(VISIBLE);
                    book_desk_img_handle2.setVisibility(INVISIBLE);
                    book_desk_img_handle3.setVisibility(INVISIBLE);


                    if (epaperInfoList.get(0).isAddEntrance()) {
//                        setAddBookImg(book_desk_img1);
                        tx_book_name1.setText("");
                        add_entran_handle1.setVisibility(VISIBLE);

                        if (isEditMode) {
                            book_desk_img_handle1.setVisibility(INVISIBLE);
                        }
                        return;
                    }

                    if (isEditMode) {
                        check_box1.setChecked(epaperInfoList.get(0).isSelectDelete());
                        if (epaperInfoList.get(0).isSelectDelete()) {
                            Log.d(TAG, "######epaperInfoList.get(0).isSelectDelete()#####" + epaperInfoList.get(0).isSelectDelete());
                            Log.d(TAG, "######epaperInfoList.get(0).getProductId()#####" + epaperInfoList.get(0).getProductId());
                        }
                    }

                    if (!TextUtils.isEmpty(epaperInfoList.get(0).getCover())) {
                        uri = Uri.parse(epaperInfoList.get(0).getCover());
                        setNetImg(book_desk_img1, uri, book_desk_img1.getWidth(), book_desk_img1.getHeight());
                    } else {
                        setLocalDefaultImg(book_desk_img1);
//                        Log.d(TAG, "######epaperInfoList.get(0).getProductName()#####"+epaperInfoList.get(0).getProductName());
                    }

                    tx_book_name1.setText(epaperInfoList.get(0).getProductName());
                    setKaoshiImgHint(img_kaoshi1, img_free1, epaperInfoList.get(0));
                    break;
                case 2:
                    book_desk_img_handle1.setVisibility(VISIBLE);
                    book_desk_img_handle2.setVisibility(VISIBLE);
                    book_desk_img_handle3.setVisibility(INVISIBLE);


                    if (!TextUtils.isEmpty(epaperInfoList.get(0).getCover())) {
                        uri = Uri.parse(epaperInfoList.get(0).getCover());

                        setNetImg(book_desk_img1, uri, book_desk_img1.getWidth(), book_desk_img1.getHeight());
//                        book_desk_img1.setImageURI(uri);
                    } else {
                        setLocalDefaultImg(book_desk_img1);
//                        Log.d(TAG, "######epaperInfoList.get(0).getProductName()#####"+epaperInfoList.get(0).getProductName());
                    }

                    tx_book_name1.setText(epaperInfoList.get(0).getProductName());
                    setKaoshiImgHint(img_kaoshi1, img_free1, epaperInfoList.get(0));
                    if (isEditMode) {

                        check_box1.setChecked(epaperInfoList.get(0).isSelectDelete());

                        if (epaperInfoList.get(0).isSelectDelete()) {
                            Log.d(TAG, "######epaperInfoList.get(0).isSelectDelete()#####" + epaperInfoList.get(0).isSelectDelete());
                            Log.d(TAG, "######epaperInfoList.get(0).getProductId()#####" + epaperInfoList.get(0).getProductId());
                        }

                    }

                    if (epaperInfoList.get(1).isAddEntrance()) {
//                        setAddBookImg(book_desk_img2);
                        add_entran_handle2.setVisibility(VISIBLE);
                        tx_book_name2.setText("");

                        if (isEditMode) {
                            book_desk_img_handle2.setVisibility(INVISIBLE);
                        }
                        return;
                    }

                    if (!TextUtils.isEmpty(epaperInfoList.get(1).getCover())) {

                        uri = Uri.parse(epaperInfoList.get(1).getCover());
                        setNetImg(book_desk_img2, uri, book_desk_img2.getWidth(), book_desk_img2.getHeight());
//                        book_desk_img2.setImageURI(uri);
                    } else {
                        setLocalDefaultImg(book_desk_img2);
//                        Log.d(TAG, "######epaperInfoList.get(1).getProductName()#####"+epaperInfoList.get(1).getProductName());
                    }


                    tx_book_name2.setText(epaperInfoList.get(1).getProductName());
                    setKaoshiImgHint(img_kaoshi2, img_free2, epaperInfoList.get(1));

                    if (isEditMode) {
                        check_box2.setChecked(epaperInfoList.get(1).isSelectDelete());

                        if (epaperInfoList.get(1).isSelectDelete()) {
                            Log.d(TAG, "######epaperInfoList.get(1).isSelectDelete()#####" + epaperInfoList.get(1).isSelectDelete());
                            Log.d(TAG, "######epaperInfoList.get(1).getProductId()#####" + epaperInfoList.get(1).getProductId());
                        }

                    }
                    break;
                case 3:
                    book_desk_img_handle1.setVisibility(VISIBLE);
                    book_desk_img_handle2.setVisibility(VISIBLE);
                    book_desk_img_handle3.setVisibility(VISIBLE);

                    if (!TextUtils.isEmpty(epaperInfoList.get(0).getCover())) {
                        uri = Uri.parse(epaperInfoList.get(0).getCover());
                        setNetImg(book_desk_img1, uri, book_desk_img1.getWidth(), book_desk_img1.getHeight());
//                        book_desk_img1.setImageURI(uri);
                    } else {
                        setLocalDefaultImg(book_desk_img1);
//                        Log.d(TAG, "######epaperInfoList.get(0).getProductName()#####"+epaperInfoList.get(0).getProductName());
                    }

                    if (!TextUtils.isEmpty(epaperInfoList.get(1).getCover())) {
                        uri = Uri.parse(epaperInfoList.get(1).getCover());
                        setNetImg(book_desk_img2, uri, book_desk_img2.getWidth(), book_desk_img2.getHeight());
//                        book_desk_img2.setImageURI(uri);
                    } else {
                        setLocalDefaultImg(book_desk_img2);
//                        Log.d(TAG, "######epaperInfoList.get(1).getProductName()#####"+epaperInfoList.get(1).getProductName());
                    }

                    tx_book_name1.setText(epaperInfoList.get(0).getProductName());
                    tx_book_name2.setText(epaperInfoList.get(1).getProductName());
                    setKaoshiImgHint(img_kaoshi1, img_free1, epaperInfoList.get(0));
                    setKaoshiImgHint(img_kaoshi2, img_free2, epaperInfoList.get(1));

                    if (isEditMode) {
                        check_box1.setChecked(epaperInfoList.get(0).isSelectDelete());
                        check_box2.setChecked(epaperInfoList.get(1).isSelectDelete());


                        if (epaperInfoList.get(0).isSelectDelete()) {
                            Log.d(TAG, "######epaperInfoList.get(0).isSelectDelete()#####" + epaperInfoList.get(0).isSelectDelete());
                            Log.d(TAG, "######epaperInfoList.get(0).getProductId()#####" + epaperInfoList.get(0).getProductId());
                        }

                        if (epaperInfoList.get(1).isSelectDelete()) {
                            Log.d(TAG, "######epaperInfoList.get(1).isSelectDelete()#####" + epaperInfoList.get(1).isSelectDelete());
                            Log.d(TAG, "######epaperInfoList.get(1).getProductId()#####" + epaperInfoList.get(1).getProductId());
                        }

                    }

                    if (epaperInfoList.get(2).isAddEntrance()) {
//                        setAddBookImg(book_desk_img3);
                        tx_book_name3.setText("");
                        add_entran_handle3.setVisibility(VISIBLE);
                        if (isEditMode) {
                            book_desk_img_handle3.setVisibility(INVISIBLE);
                        }
                        return;
                    }

                    if (!TextUtils.isEmpty(epaperInfoList.get(2).getCover())) {
                        uri = Uri.parse(epaperInfoList.get(2).getCover());
                        setNetImg(book_desk_img3, uri, book_desk_img3.getWidth(), book_desk_img3.getHeight());
//                        book_desk_img3.setImageURI(uri);
                    } else {
                        setLocalDefaultImg(book_desk_img3);
//                        Log.d(TAG, "######epaperInfoList.get(2).getProductName()#####"+epaperInfoList.get(2).getProductName());
                    }

                    tx_book_name3.setText(epaperInfoList.get(2).getProductName());
                    setKaoshiImgHint(img_kaoshi3, img_free3, epaperInfoList.get(2));

                    if (isEditMode) {
                        check_box3.setChecked(epaperInfoList.get(2).isSelectDelete());
                        if (epaperInfoList.get(2).isSelectDelete()) {
                            Log.d(TAG, "######epaperInfoList.get(2).isSelectDelete()#####" + epaperInfoList.get(2).isSelectDelete());
                            Log.d(TAG, "######epaperInfoList.get(2).getProductId()#####" + epaperInfoList.get(2).getProductId());
                        }
                    }
                    break;

            }
        } else {
            setVisibility(GONE);
        }

    }


    private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri, int width, int height) {
        try {
            ImageRequest request = null;
            if (uri != null) {
                request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(getContext(), 300), DisplayUtils.dip2px(getContext(), 420))).build();

                DraweeController drawweeController = Fresco.newDraweeControllerBuilder().setUri(uri).setImageRequest(request)
                        //列表滑动会变流畅
                        .setOldController(simpleDraweeView.getController())
                        //                    .setAutoPlayAnimations(true)
                        .build();
                simpleDraweeView.setController(drawweeController);
            }
        } catch (Exception e) {

        } catch (OutOfMemoryError outOfMemoryError) {

        }


    }

    private void setKaoshiImgHint(ImageView kaoShi, ImageView imgFree, EpaperInfo epaperInfo) {


        if (null != epaperInfo) {
            kaoShi.setVisibility(epaperInfo.getRequired() == 1 ? View.VISIBLE : View.INVISIBLE);
            imgFree.setVisibility(epaperInfo.getIsFree() == 1 ? View.VISIBLE : View.INVISIBLE);
        }

    }


    private void setLocalDefaultImg(SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse("res:///" + R.mipmap.item_load_icon);
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setTapToRetryEnabled(true)
                .build();
        simpleDraweeView.setController(controller);
    }


    private void setAddBookImg(SimpleDraweeView simpleDraweeView) {
        Uri uri = Uri.parse("res:///" + R.mipmap.icon_add_service);
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setTapToRetryEnabled(true)
                .build();
        simpleDraweeView.setController(controller);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (R.id.book_desk_img_handle1 == id) {

            if (null != mEpaperInfoList && mEpaperInfoList.size() > 0 && !mEpaperInfoList.get(0).isAddEntrance()) {

                if (mIsEidtMode) {
                    check_box1.setChecked(!check_box1.isChecked());
                    return;
                }
            } else {
                //add book
                EpaperJumpManager.jumpToAddBook(R.string.go_back, mFragment.getActivity(),
                        mFragment, EpaperFragment.REQ_CODE_TO_ADD_BOOK);
            }

        } else if (R.id.book_desk_img_handle2 == id) {
            if (null != mEpaperInfoList && mEpaperInfoList.size() > 1 && !mEpaperInfoList.get(1).isAddEntrance()) {
                if (mIsEidtMode) {
                    check_box2.setChecked(!check_box2.isChecked());
                    return;
                }
            } else {
                EpaperJumpManager.jumpToAddBook(R.string.go_back, mFragment.getActivity(),
                        mFragment, EpaperFragment.REQ_CODE_TO_ADD_BOOK);
            }
        } else if (R.id.book_desk_img_handle3 == id) {
            if (null != mEpaperInfoList && mEpaperInfoList.size() > 2 && !mEpaperInfoList.get(2).isAddEntrance()) {
                if (mIsEidtMode) {
                    check_box3.setChecked(!check_box3.isChecked());

                    return;
                }
            } else {
                //add book
                EpaperJumpManager.jumpToAddBook(R.string.go_back, mFragment.getActivity(),
                        mFragment, EpaperFragment.REQ_CODE_TO_ADD_BOOK);
            }

        } else if (id == R.id.add_entran_handle1 || id == R.id.add_entran_handle2 || id == R.id.add_entran_handle3) {
            //add book
            EpaperJumpManager.jumpToAddBook(R.string.go_back, mFragment.getActivity(),
                    mFragment, EpaperFragment.REQ_CODE_TO_ADD_BOOK);
        }

    }

    @Override
    public boolean onLongClick(View v) {
//        int id=v.getId();
//
//        if (R.id.book_desk_img_handle1 == id || R.id.book_desk_img_handle2 == id|| R.id.book_desk_img_handle3 == id) {
////            EventBus.getDefault().post(new AddEvent(EpaperFragment.JUMP_TO_EDIT_BOOK));
//
//        }

        return true;
    }
}
